package com.cqupt.controller;

import com.cqupt.pojo.ResponseMessage;
import com.cqupt.service.DashBoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version
 * @description:
 * @date
 */

@CrossOrigin
@RequestMapping("dashboard")
@RestController
@ResponseBody
public class DashBoardController {

    @Autowired
    private DashBoardService dashBoardService;


    /**
     * @description: 获取各个状态下的主机数量
     * @param:
     * @return:
     * @author
     * @date:
     */
    @RequestMapping("/get_numbers_of_host_in_different_type")
    public ResponseMessage getNumbersOfHostInDifferentType(){
        ResponseMessage responseMessage = new ResponseMessage();
        final Map<String, Object> resultList = dashBoardService.getNumbersOfHostInDifferentType();
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", resultList);
        }};
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }


    /**
     * @description: 获取各个告警状态下的问题数量
     * @param:
     * @return:
     * @author
     * @date:
     */
    @RequestMapping("/getNumberOfProblemInDifferentType")
    public ResponseMessage getNumberOfProblemInDifferentType(){
        ResponseMessage responseMessage = new ResponseMessage();
//      告警状态级别包括 未分类、信息类、警告、一般严重、严重、灾难
        int NotClassified=0,Information=0,Warning=0,Average=0,High=0,Disaster=0;
        List<String> problems = new ArrayList<>();
//      获取所有不同类型的告警
        final List<Map<String, Object>> resultList = dashBoardService.getNumberOfProblemDifferentType();
        for (Map<String,Object> maps:resultList){
           problems.add((String) maps.get("severity"));
        }
//      计算各告警级别数量
        for (int i=0;i<problems.size();++i){
            switch (problems.get(i)){
                case "0":
                    NotClassified++;
                    break;
                case "1":
                    Information++;
                    break;
                case "2":
                    Warning++;
                    break;
                case "3":
                    Average++;
                    break;
                case "4":
                    High++;
                    break;
                case "5":
                    Disaster++;
                    break;
                default:
                    break;
            }
        }
        int finalNotClassified = NotClassified; // 未定义
        int finalInformation = Information;     //  信息
        int finalWarning = Warning;             //  警告
        int finalAverage = Average;             //  一般严重
        int finalDisaster = Disaster;           //   严重
        int finalHigh = High;                   //   灾难
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("NotClassified", finalNotClassified);
            put("Information", finalInformation);
            put("Warning", finalWarning);
            put("Average", finalAverage);
            put("High", finalHigh);
            put("Disaster", finalDisaster);
        }};
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }
    /**
     * @description: 根据cpu 内存利用率统计主机数量
     * @param:
     * @return:
     * @author
     * @date:
     */
    @RequestMapping("/get_info_classified_by_cpu_and_memory_utilization")
    public ResponseMessage getInfoClassifiedByCpuAndMemoryUtilization(){

        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> result = dashBoardService.getInfoClassifiedByCpuAndMemoryUtilization();
        responseMessage.setStatus(true);
        responseMessage.setContent(result);
        return responseMessage;
    }

    /**
     * 获取高负载设备列表
     * @param params
     * @return
     */
    @RequestMapping("/get_high_load_host_list")
    private ResponseMessage getHighLoadHostList(@RequestBody Map<String, Object> params){
        String name = params.get("name").toString();
        ResponseMessage responseMessage = new ResponseMessage();
        List<Map<String, Object>> resultList = dashBoardService.getHighLoadList(name);
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", resultList);
        }};
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }
}
